import 'dart:convert';

import 'package:acgn_pioneer/config/app_config.dart';
import 'package:crypto/crypto.dart';
import 'package:encrypt/encrypt.dart' as encrypt;

class CryptoUtil {
  // 解密
  static String aesDecrypt(String data) {
    final encrypter = encrypt.Encrypter(encrypt.AES(
        encrypt.Key.fromUtf8(AppConfig.AES_KEY),
        mode: encrypt.AESMode.cbc));
    return encrypter.decrypt64(data.replaceFirst(AppConfig.AES_DATA, ''),
        iv: encrypt.IV.fromUtf8(AppConfig.AES_IV));
  }

  // 加密
  static String aesEncrypt(String data) {
    final encrypter = encrypt.Encrypter(encrypt.AES(
        encrypt.Key.fromUtf8(AppConfig.AES_ENCRYPT_KEY),
        mode: encrypt.AESMode.ecb));
    final encrypted = encrypter.encrypt(data,
        iv: encrypt.IV.fromUtf8(AppConfig.AES_ENCRYPT_IV));
    return encrypted.base64;
  }

  // md5加密
  static String md5Encrypt(String data) {
    var content = new Utf8Encoder().convert(data);
    var digest = md5.convert(content);
    return digest.toString();
  }
}
